package com.taoban.shopdata.core;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.message.BasicNameValuePair;

import com.taoban.shopdata.entity.PriceUpdatedCommitResult;
import com.taoban.shopdata.utils.ConfigUtils;
import com.yufei.dataretriver.DataRetrieverFactory;
import com.yufei.dataretriver.DataRetrieverFeatures;
import com.yufei.dataretriver.HttpDataRetriever;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.DateUtil;


/**
 * @author jasstion
 * 下午1:52:56
 * @param <I>:h商品标识
 * @param <R>:解析返回的商品信息（价格，库存，商品其他信息等等）
 */
public abstract class AbstractShopDataConsumer<I,R> {
	protected static Log mLog = LogFactory.getLog(AbstractShopDataConsumer.class);

	protected List<I> itemInfos = new ArrayList<I>();
	protected List<Double> prices = new ArrayList<Double>();

	protected PriceUpdatedCommitResult priceUpdatedCommitResult = null;

	//public abstract void commitItemPrice();

	public abstract void process();

	/**
	 * @param itemInfo 商品信息（各个商城数据接口获取商品信息时候要求传入的商品标识）
	 * @return
	 * 批量获取商品价格信息
	 */
	public abstract List<R> getItemPriceByItemInfo(List<I> itemInfo);

	public void commitItemPrice() {
	
	    mLog.info("开始提交价格更行信息，时间为:"+DateUtil.getCurrentDateString()+"");
	    mLog.info("此次提交更行的商品个数为:"+itemInfos.size()+"");
		 DataRetrieverFeatures dataRetrieverFeatures = new DataRetrieverFeatures(false, "post");
		 dataRetrieverFeatures.setRequestTimeout(Integer.parseInt(ConfigUtils.getVlaue("//commitTaskResultTimeOut")));
		 dataRetrieverFeatures.setConnectionTimeout(1000*60*3);
		HttpDataRetriever dataRetriever = DataRetrieverFactory
					.createDataRetriever(dataRetrieverFeatures);
		try {
			priceUpdatedCommitResult.setItemInfos(itemInfos);
			priceUpdatedCommitResult.setPrices(prices);
		String content=CommonUtil.generateXmlForObject(priceUpdatedCommitResult, PriceUpdatedCommitResult.class);
	
	    String urlForCommitUpdatedPrice = null;
		mLog.debug("此次提交的价格更行的内容是:"+content+",commitUrl="+urlForCommitUpdatedPrice+"");
	    try {
	    	//Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml")
			urlForCommitUpdatedPrice=ConfigUtils.getUrlForCommitUpdatedPrice();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			mLog.info("read the config.xml Error for get urlForCommitUpdatedPrice!");
			return;
		} 
	    
		dataRetriever.setUrl(new URL(urlForCommitUpdatedPrice));
		List<BasicNameValuePair> formParams=new ArrayList<BasicNameValuePair>();
		formParams.add(new BasicNameValuePair("action", "commitUpdatedPrice"));
	
		formParams.add(new BasicNameValuePair("xmlContent", content));
		dataRetriever.setFormParams(formParams);
		dataRetriever.connect();
		
			mLog.info(dataRetriever.getHtmlContent());
			dataRetriever.disconnect();
	        mLog.info("提交价格更行信息成功，时间为:"+DateUtil.getCurrentDateString()+"");
	
	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			mLog.info("提交价格更行信息失败,具体异常信息是:");
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
	/*	PriceUpdatedCommitResult priceUpdatedCommitResult = PriceUpdatedTaskRequest
				.requestBatchItemInfos();
		AbstractShopDataConsumer abstractShopDataConsumer = FactoryUtils
				.getShopDataConsumer(priceUpdatedCommitResult);
		abstractShopDataConsumer.process();
		long dat=(long)1363055819566.00;
		System.out.print(new Date(dat).toGMTString());
		// B007VJXUIC
*/
		String str="http://union.yihaodian.com/api/productInfo/yihaodian/product/5177728-1927767.xml";
		try {
			System.out.print(CommonUtil.getHtmlContent(str, false));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
